package ru.creator.logical.links.db.tables;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Types;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import ru.creator.logical.links.db.Table;
import ru.creator.logical.links.db.TableObject;




/**
 *
 * @author ~jo-MA-jo~
 */
public class Subjects extends Table {
    public int id = 0;
    public String subject = "";
    public Set<Integer> landmark = new HashSet<Integer>();

    
    public Subjects (int id, String subject, Set<Integer> landmark) {
        this.id = id;
        this.subject = subject;
        this.landmark = landmark;
        init();
    }
   
    public Subjects () {
        init();
    }
   
    public void init(){
        name="subjects";
        structure = new LinkedHashMap();
        structure.put("id", new TableObject(Types.INTEGER,id));
        structure.put("subject", new TableObject(Types.VARCHAR,subject));
        structure.put("landmark", new TableObject(Types.JAVA_OBJECT,landmark));
    }
    
    @Override
    public String getTableName(){
       return this.name;
    }
    
    @Override
    public LinkedHashMap<String, TableObject> getStructure() {
        return structure;
    }
    
    @Override   
    public Table getTableObject(Connection db, String q){
        Subjects s = new Subjects();
        ResultSet rSet=null;
        Statement stmt=null;
        try{
            stmt = db.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            rSet = stmt.executeQuery(q);
            rSet.next();
            s.id = rSet.getInt(1);
            s.subject= rSet.getString(2);
            byte[] data = rSet.getBytes(3);
            s.landmark = (Set<Integer>) TableObject.toObject(data);            
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
        	if(rSet!=null) try{rSet.close();} catch(Exception e) {};
        	if(stmt!=null) try{stmt.close();} catch(Exception e) {};
        }
        return s;
    }


    public List<Table> getTableList(Connection db, String q){
        List<Table> list = new ArrayList<Table>();
        ResultSet rSet=null;
        Statement stmt=null;
        try{
        	stmt = db.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        	rSet = stmt.executeQuery(q);
            while(rSet.next()) {
                Subjects s = new Subjects();
                s.id = rSet.getInt(1);
                s.subject= rSet.getString(2);
                byte[] data = rSet.getBytes(3);
                s.landmark = (Set<Integer>) TableObject.toObject(data);
                list.add(s);
            }
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
        	if(rSet!=null) try{rSet.close();} catch(Exception e) {};
        	if(stmt!=null) try{stmt.close();} catch(Exception e) {};
        }
        return list;
    }
    
}
